# ImageMenuItem - Menuitem that contains image
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import clutter
from frontend.gui.widgets.menu_item import MenuItem

class ImageMenuItem(MenuItem):
    """
    Simple image menuitem
    
    This menu item contains one clutter.Texture that is cropped in such a way
    that texture fills the whole menu item area. Texture aspect ratio is not
    changed!
    """
    
    def __init__(self, width, height, texture):
        """
        Initialize menuitem.
        @param width: Width of the menuitem
        @param height: Height of the menuitem
        """
        MenuItem.__init__(self, width, height)
        texture.set_size(width, height)
        texture.show()
        self.add(texture)

# ALTERNATIVE CONSTRUCTOR. THIS CROPS TEXTURE   
#    def __init__(self, width, height, texture):
#        """
#        Initialize menuitem.
#        @param width: Width of the menuitem
#        @param height: Height of the menuitem
#        """
#        texture.set_name("item_texture")
#        MenuItem.__init__(self, width, height)
#        self.set_clip(0, 0, width, height)
#        w, h = texture.get_size()
#        
#        if w != width or h != height:
#            x_ratio = width / float(w)
#            y_ratio = height / float(h)
#            if x_ratio < y_ratio:
#                texture.set_scale(height / float(h), 
#                                  height / float(h))
#                new_width = int(w * (height / float(h)))
#                new_x = int((width - new_width) / float(2))
#                texture.set_position(new_x, 0)
#            else:
#                texture.set_scale(width / float(w), 
#                                  width / float(w))
#                new_height = int(h * (width / float(w)))
#                new_y = int((height - new_height) / float(2))
#                texture.set_position(0, new_y)
#
#        texture.show() 
#        self.add(texture)
        
#    def set_active(self, boolean):
#        MenuItem.set_active(self, boolean)
#        if boolean:
#            for index in range(self.get_n_children()):
#                actor = self.get_nth_child(index)
#                if actor.get_name() == "item_texture":
#                    actor.set_scale_with_gravity(1.2, 1.2, clutter.GRAVITY_CENTER)
#        else:
#            for index in range(self.get_n_children()):
#                actor = self.get_nth_child(index)
#                if actor.get_name() == "item_texture":
#                    actor.set_scale_with_gravity(1.0, 1.0, clutter.GRAVITY_CENTER)